const AddressModel = require("../models/addressModel");

async function addressCreate(username, data) {
  const res = AddressModel.create({
    username,
    ...data,
  });
  return res;
}

async function getAllAddress(username) {
  const res = AddressModel.find({ username }).sort({ updatedAt: -1 });
  return res;
}

async function getOneAddress(id) {
  const res = AddressModel.findById(id);
  return res;
}

async function getOneAndUpdate(id, username, data) {
  const res = await AddressModel.findOneAndUpdate(
    { _id: id, username },
    { username, ...data },
    {
      new: true,
    }
  );
  return res;
}

module.exports = {
  addressCreate,
  getAllAddress,
  getOneAddress,
  getOneAndUpdate,
};
